package com.wuhan.template.module.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wuhan.template.base.BaseApplication;
import com.wuhan.template.module.find.webview.WebViewActivity;
import com.wuhan.template.module.home.entity.JiaoYuEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/11 0011.
 */
public class JiaoYuPageAdapter extends PagerAdapter {

    private List<JiaoYuEntity.SlideBean> slideBeans;
    private Context context;
    private LayoutInflater inflater;
    private ImageLoader loader;
    private DisplayImageOptions options;
    private List<ImageView> imageViews  = new ArrayList<>();

    public JiaoYuPageAdapter(Context context, List<JiaoYuEntity.SlideBean> slideBeans) {
        this.slideBeans = slideBeans;
        this.context = context;
        inflater = LayoutInflater.from(context);
        loader = ((BaseApplication) (context.getApplicationContext())).getImageLoader();
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();
        for (int i = 0; i < slideBeans.size(); i++) {
            ImageView imageView = new ImageView(context);
            imageViews.add(imageView);
        }
    }


    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        position %= slideBeans.size();
        if (position < 0) {
            position = slideBeans.size() + position;
        }
        final JiaoYuEntity.SlideBean slideBean = slideBeans.get(position);
        String imageUrl = slideBean.getIndexpic().getHost() + slideBean.getIndexpic().getDir() +
                slideBean.getIndexpic().getFilepath() + slideBean.getIndexpic().getFilename();
        ImageView imageView = imageViews.get(position);
        loader.displayImage(imageUrl, imageView, options);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        ViewParent parent = imageView.getParent();
        if(parent!=null){
            ViewGroup group = (ViewGroup) parent;
            group.removeView(imageView);
        }

        container.addView(imageView);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("url",slideBean.getContent_url());
                intent.putExtra("title",slideBean.getColumn_name());
                context.startActivity(intent);
            }
        });
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}
